package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.EncyclopediaService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Created by xiaoji on 2016/4/28.
 * 百度百科
 */
@Controller
@RequestMapping(value = "/encyclopediaControl")
public class EncyclopediaControl {
    private static Logger log = LoggerFactory.getLogger(EncyclopediaControl.class);
    @Autowired
    private EncyclopediaService encyclopediaService;

    @Autowired
    private ConsumerService consumerService;

    /**
     *  获取百科分类
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/category",method = RequestMethod.GET)
    public void category(ModelMap modelMap,@RequestParam Map<String,String> param) {
        try {
            modelMap.addAttribute("category",encyclopediaService.getCategory());
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取百科列表
     * @param modelMap
     * @param param
     * @param s 开始序列
     * @param n 获取条数
     */
    @RequestMapping(value = "/encyclopediaList",method = RequestMethod.GET)
    public void encyclopediaList(ModelMap modelMap,@RequestParam Map<String,String> param,Integer s,Integer n) {
        try {
            modelMap.addAttribute("encyclopediaList",encyclopediaService.encyclopediaList(param.get("name"),Integer.parseInt(param.get("category_id")),s,n));
        }catch (Exception e){
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取百科内容
     * @param modelMap
     * @param param
     * @param encyclopedia_id 百科id
     */
    @RequestMapping(value = "/getEncyclopediaContent",method = RequestMethod.GET)
    public void getEncyclopediaContent(ModelMap modelMap,@RequestParam Map<String,String> param,Long encyclopedia_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String,Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("articleContent",encyclopediaService.getEncyclopediaContent(String.valueOf(map.get("id")),encyclopedia_id));
        }catch (Exception e){
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
}
